//
//  WBimage + Extension.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/20.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit

extension  UIImage{
    
    class  func snapShopSitImage()-> UIImage{
        //截屏
        let window = UIApplication.shared.keyWindow!
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(window.frame.size, false, 0)
        //绘制图形上下文
        window.drawHierarchy(in: window.frame, afterScreenUpdates: true)
        //获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    //将图片缩放到指定宽度
    func scaleImage(width:CGFloat)->UIImage{
        let itemWidth = self.size.width
        let itemHight = self.size.height
        //如果图片的宽度小于缩放的宽度,不需要再缩放
        if itemWidth < width {
            return self
        }
//        计算缩放后的H
       let hight = itemHight * width / itemWidth
        let imageBounds = CGRect(x: 0, y: 0, width: width, height: hight)
        
        //开启图片上下文
        UIGraphicsBeginImageContextWithOptions(imageBounds.size, false, 0)
        //绘制图形上下文
        draw(in: imageBounds)
        //获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭
        UIGraphicsEndImageContext()
        return image!

    }
    
}






